package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.module.ADUserModule;

public class DoUser extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String action=request.getParameter("action");
		ADUserModule adUserModule=new ADUserModule();
		
		if(action.equals("update")){
			String username=request.getParameter("username");
			String password=request.getParameter("password");
			int userid=Integer.parseInt(request.getParameter("userid"));
			int flag;
			try {
				flag = adUserModule.updateAction(username, password, userid);
				if(flag==1){
					response.sendRedirect("/CuitShopPlatform/admin/userManage.jsp");
				}
				else{
					System.out.print("error");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		else if (action.equals("insert")) {
			String username=request.getParameter("username");
			String password=request.getParameter("password");
			System.out.print(username);
			int flag;
			try {
				flag = adUserModule.insertAction(username, password);
				System.out.print(flag);
				if(flag==1){
					response.sendRedirect("/CuitShopPlatform/admin/userManage.jsp");
				}
				else{
					System.out.print("error");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		else if (action.equals("del")) {
			
			String id=request.getParameter("userid");
			int userid=Integer.parseInt(id);
			System.out.print(userid);
			int flag;
			try {
				flag = adUserModule.delAction(userid);
				if(flag==1){
					response.sendRedirect("/CuitShopPlatform/admin/userManage.jsp");
				}
				else{
					System.out.print("error");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		out.flush();
		out.close();
	}

}
